<template>
  <div style="width: 600px;background: #fff;">
    <el-breadcrumb separator-class="el-icon-arrow-right">

      <el-breadcrumb-item :to="{ path: '/sys-admin' }"><i class="el-icon-s-promotion"></i>后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>修改公告</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="id" prop="id">
        <el-input v-model="ruleForm.id" disabled="disabled"></el-input>
      </el-form-item>

      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>

      <el-form-item label="公告正文" prop="text">
        <el-input v-model="ruleForm.text" ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">修改</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        id: '',
        name: '',
        text: '',
        modified:''
      }
    };
  },
  methods: {
    submitForm(formName) {
      console.log("......"+this.getNowTime());
      this.ruleForm.modified=this.getNowTime();
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:8090/announcement/'+ this.ruleForm.id +'/update';
          console.log('url = ' + url);
          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData：' + formData);
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response)=> {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              this.$message({
                message: '修改公告成功！',
                type: 'success'
              });
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    loadAnnouncement(){
      let id = this.$route.query.id
      let url = "http://localhost:8090/announcement/"+id;
      console.log("----"+url);
      this.axios.get(url).then((response) =>{
        let responseBody = response.data
        this.ruleForm = responseBody.data;
        console.log(responseBody);
      });
    },

    //获取当前时间
    getNowTime() {
      let date = new Date();
      //年 getFullYear()：四位数字返回年份
      let year = date.getFullYear();  //getFullYear()代替getYear()
      //月 getMonth()：0 ~ 11
      let month = date.getMonth() + 1;
      //日 getDate()：(1 ~ 31)
      let day = date.getDate();
      //时 getHours()：(0 ~ 23)
      let hour = date.getHours();
      //分 getMinutes()： (0 ~ 59)
      let minute = date.getMinutes();
      //秒 getSeconds()：(0 ~ 59)
      let second = date.getSeconds();

      let time =  year + '-' + this.addZero(month) + '-' + this.addZero(day) + ' ' + this.addZero(hour) + ':' + this.addZero(minute) + ':' + this.addZero(second);
      return time;
    },
    //小于10的拼接上0字符串
    addZero(s) {
    return s < 10 ? ('0' + s) : s;
  }
  },
  mounted() {
    this.loadAnnouncement();
  }
}
</script>

<style scoped>

</style>